// n = 9, max = 34, array = [6, 6, 6, 8, 8, 8, 5, 5, 1]
//n = 9, max = 37, array = [9, 9, 9, 9, 6, 6, 6, 6, 13]
// n = 9, max = 40, array = [1, 11, 13, 12, 7, 8, 11, 5, 6]
//n = 6, max = 50, array = [13, 13, 13, 1, 1, 1]
n = 31, max = 42, array = [3, 3, 11, 12, 12, 2, 13, 5, 13, 1, 13, 8, 8, 1, 8, 13, 12, 9, 2, 11, 3, 5, 8, 11, 1, 11, 1, 5, 4, 2, 5]

function solution(n, max, array) {
    // Edit your code here
    let Hash = {}
    let hulu = [0, 0]
    let huluMax = 0
    for (let i = 0; i < n; i++) {
        if (!Hash[array[i]]) {
            Hash[array[i]] = 0
        }
        Hash[array[i]]++
    }
    console.log(Hash);
    let two = []
    let three = []
    for (var i in Hash) {
        if (Hash[i] == 2) {
            two.push(parseInt(i))

        } else if (Hash[i] >= 3) {
            three.push(parseInt(i))
        }
    }
    console.log(two, three);
    let twoLen = two.length, threeLen = three.length
    if (!threeLen) return [0, 0]
    for (let i = 0; i < twoLen; i++) {
        for (let j = 0; j < threeLen; j++) {
            let middle = three[j] * 3 + two[i] * 2
            if (middle <= max && middle >= huluMax && hulu[0] != 1 && hulu[1] != 1) {
                hulu[0] = three[j]
                hulu[1] = two[i]
                huluMax = middle
            }
        }
    }
    for (let i = 0; i < threeLen; i++) {
        for (let j = 0; j < threeLen; j++) {
            if (i !== j) {
                let sum = three[i] * 3 + three[j] * 2;
                if (sum <= max && sum > huluMax) {
                    hulu[0] = three[i];
                    hulu[1] = three[j];
                    huluMax = sum;
                    console.log(hulu);

                }
            }
        }
    }
    console.log(hulu);
    return hulu
}
console.log(solution(n, max, array));
